<?php
declare(strict_types=1);
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/12
 * Time: 14:40
 */

namespace Modules\Api\Service;
use HiCommon\Repository\AddressRepository;
use HiCommon\Repository\AreasRepository;

class AddressService extends BaseService
{

    /**
     *  获取用户的所有地址
     *  hinq 2019 11 12
     *
     **/
    public function get_all(){
        try{
            $user_id = api_user_id();
            $arr_option['where'] = ['user_id'=>$user_id , 'status'=>1];
            $arr_option['field'] = ['id','user_id','username','phone','province','city','area','address','is_defaule'];
            $arr_option['order'] = ['key'=>'created_at','vlaue'=>'desc'];
            $arr_list = AddressRepository::get_all( (array) $arr_option );
            return hello_success('ok',$arr_list);
        }catch ( \Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  获取用户的默认地址
     *  hinq 2019 12 19
     *
     **/
    public function get_defaule_address(){
         $user_id = api_user_id();
         $where = ['is_defaule'=>1,'user_id'=>$user_id , 'status'=>1];
         $arr_option['field'] = ['id','user_id','username','phone','province','city','area','address','is_defaule'];
         $arr_list =  AddressRepository::get_one_to_where( (array) $where , (array) $arr_option );
         if( empty( $arr_list->first() ) ){
             return hello_success('ok',[]);
         }
         return hello_success('ok',$arr_list[0]);
    }

    /**
     *  验证收货地址
     *  hinq 2019 10 15
     **/
    public function check_data( array $arrInput ){
        if( empty( $arrInput['username'] ) ){
            return ['state'=> 1,'msg'=>'收货人姓名不能为空','data'=>[],'error'=>''];
        }
        if( empty( $arrInput['phone'] ) ){
            return ['state'=> 1,'msg'=>'收货人手机号码不能为空','data'=>[],'error'=>''];
        }
        if( !preg_match("/^1[3456789]\d{9}$/", $arrInput['phone'] ) ){
            return hello_error('老板手机号码格式不正确');
        }
        if( empty( $arrInput['province'] ) ){
            return ['state'=> 1,'msg'=>'请选择省份','data'=>[],'error'=>''];
        }
        if( empty( $arrInput['city'] ) ){
            return ['state'=> 1,'msg'=>'请选择城市','data'=>[],'error'=>''];
        }
        if( empty( $arrInput['area'] ) ){
            return ['state'=> 1,'msg'=>'请选择区县','data'=>[],'error'=>''];
        }
        if( empty( $arrInput['address'] ) ){
            return ['state'=> 1,'msg'=>'详细地址不能为空','data'=>[],'error'=>''];
        }
        if( mb_strlen( $arrInput['address'] ) > 50 ){
            return ['state'=> 1,'msg'=>'详细地址最多50个字符','data'=>[],'error'=>''];
        }
        return $arrInput;
    }

    /**
     *	添加收货地址
     *	hinq 2019 11 12
     *
     **/
    public function store( array $arr_input ){
        \DB::beginTransaction();
        try{
            $temp = self::check_data( (array) $arr_input );
            if( $temp['state'] == 1 ){
                return $temp;
            }
            $user_id = api_user_id();
            $data['user_id'] = $user_id;
            $data['username'] = $temp['username'];
            $data['phone'] = $temp['phone'];
            $data['province'] = $temp['province'];
            $data['city'] = $temp['city'];
            $data['area'] = $temp['area'];
            $data['address'] = $temp['address'];
            $data['is_defaule'] = $temp['is_defaule'];
            $data['status'] =1;
            $res = true;

            if( $data['is_defaule'] == 1 ){ //当前的数据需要设为默  将之前设置的默认地址改成非默认
                //将之前设置的默认地址修改为非默认
                $arr_where['user_id'] = $user_id;
                $arr_where['is_defaule'] = 1;
                $arr_option['is_defaule'] = 0;
                $res = AddressRepository::edit_one_where( (array) $arr_where , (array) $arr_option );
                if( $res == 0 ){   //有可能之前没有设置默认地址 返回值为0 影响后面判断 但是不能影响程序正常执行
                    $res = true;
                }
            }
            $res2 = AddressRepository::add_one( (array) $data );
            if( $res && $res2 ){
                \DB::commit();
            }
            return hello_success('ok');
        }catch ( \Exception $exception){
            \DB::rollBack();
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *	删除收货地址
     *	hinq 2019 11 12
     *
     **/
    public function delete( int $nId ){
        try{
            if( empty( $nId ) ){
                return hello_error('参数错误');
            }
            $user_id = api_user_id();
            $where = [ 'user_id' => $user_id , 'id' => $nId ,'status'=>1];
            $count = AddressRepository::get_count_by_where( (array) $where );
            if( $count == 0 ){
                return hello_error('删除失败');
            }
//            $res = AddressRepository::delete_one( (int) $nId );
            $arr_option['status'] = 2;
            //地址信息不能真删除  不然订单关联的收货地址信息就会查询不到
            $res = AddressRepository::edit_one_where( (array) $where , (array) $arr_option );
            if( empty( $res ) ){
                return hello_error('删除失败');
            }
            return hello_success('ok');
        }catch ( \Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  获取指定的收货地址信息
     *  hinq 2019 11 12
     *
     **/
    public function get_one( int $nId ){
        try{
            if( empty( $nId ) ){
                return hello_error('参数错误');
            }
            $user_id = api_user_id();
            $where = [ 'user_id' => $user_id , 'id' => $nId ,'status'=>1];
            $count = AddressRepository::get_count_by_where( (array) $where );
            if( $count == 0 ){
                return hello_error('参数错误');
            }
            $arr_list = AddressRepository::get_one( (int) $nId , (array) $where );
            return hello_success('ok',$arr_list);
        }catch ( \Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  更新指定的收货地址信息
     *  hinq 2019 11 12
     *
     **/
    public function update( array $arr_input ){
        \DB::beginTransaction();
        try{
            $temp = self::check_data( (array) $arr_input );
            if( $temp['state'] == 1 ){
                return $temp;
            }


            $user_id = api_user_id();
//            $data['user_id'] = $user_id;
            $data['username'] = $temp['username'];
            $data['phone'] = $temp['phone'];
            $data['province'] = $temp['province'];
            $data['city'] = $temp['city'];
            $data['area'] = $temp['area'];
            $data['address'] = $temp['address'];
            $data['is_defaule'] =  $temp['is_defaule'];
            $data['id'] = $temp['id'];
            $res = true;
            if( $data['is_defaule'] == 1 ){ //当前的数据需要设为默
                $arr_where['user_id'] = $user_id;
                $arr_where['is_defaule'] = 1;
                //将之前设置的默认地址修改为非默认
                $arr_option['is_defaule'] = 0;
                $res = AddressRepository::edit_one_where( (array) $arr_where , (array) $arr_option );
                if( $res == 0 ){   //有可能之前没有设置默认地址 返回值为0 影响后面判断 但是不能影响程序正常执行
                    $res = true;
                }
            }
            $res2 = AddressRepository::edit_one( (array) $data );
            if( $res && $res2 ){
                \DB::commit();
            }
            return hello_success('ok');
        }catch ( \Exception $exception){
            \DB::rollBack();
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }


    /**
     *  获取所有省份信息列表
     *  hinq 2019 11 21
     *
     **/
    public function get_address_list( array $arr_input ){
        $arr_input ['level'] = empty( $arr_input ['level'] ) ? 1 : $arr_input ['level'];
        $arr_input ['id'] = empty( $arr_input ['id'] ) ? 100000 : $arr_input ['id'];
        $arr_option['where'] = ['level'=> $arr_input ['level'] , 'pid'=>$arr_input ['id']];
        $arr_option['order'] = ['key'=>'id','value'=>'asc'];
        $arr_option['field'] = ['id','name','Lng','Lat','pid'];
        $arr_list = AreasRepository::get_all( (array) $arr_option );
        return hello_success('ok',$arr_list);
    }


}